$(document).ready(function () {
    loading(false);
    setTimeout("setPage(0)", 1000)
});
var page = 0;
var total;

function setPage(state) {
    switch (state) {
        case 0:
            page = 1;
            break;
        case 1:
            if (page > 1) {
                page--;
            }
            break;
        case 2:
            if (page < total) {
                page++;
            }
            break;
        case 3:
            page = total;
            break;
    }
    getList(page);
}

/**
 * 获取用户列表
 */
function getList(arg0) {
    $.ajax({
        type: "POST",
        url: "/home",
        data: JSON.stringify(createJson(arg0)),
        contentType: "application/json;charset:utf-8",
        dataType: "json",
        success: function (data) {
            loading(true);
            if (data.code == 0) {
                total = data.total;
                createJobElement(data.data);
            } else {
                alert(data.message);
            }
        }, error: function (message) {
            loading(true);
            alert(message);
        }
    })
}

/**
 * 组装参数
 * @param arg0
 * @returns {{param: string, page: *}}
 */
function createJson(arg0) {
    var json = {
        "param": "companyList",
        "page": arg0
    };
    return json;
}

/**
 * 构建用户列表HTML
 */
function createJobElement(list) {
    $("#page-index").text("第" + page + "页/共" + total + "页");
    if (page == total) {
        $("#footer").find("h4:nth-child(4)").hide(500);
    } else {
        $("#footer").find("h4:nth-child(4)").show(500);
    }

    if (page == 1) {
        $("#footer").find("h4:nth-child(3)").hide(500);
    } else {
        $("#footer").find("h4:nth-child(3)").show(500);
    }

    $("#user-table").empty();
    $(list).each(function (index, comp) {
        var item = "<tr class='user-item'>" +
            "<th>"
            + comp.compNo
            + "</th>" +
            "<th>"
            + comp.compName
            + "</th>" +
            "<th>"
            + comp.compCreate
            + "</th>" +
            "<th>"
            + comp.scope
            + "</th>" +
            "<th><a href='" + comp.link + "'>"
            + "网址</a></th>" +
            "<th>修改</th><th>删除</th>"
        "</tr>";
        $(item).appendTo("#user-table");
    })
}

/**
 * 导航点击事件
 */
function navClick(index) {
    switch (index) {
        case 1:
            window.location = "userManager.html";
            break
        case 3:
            window.location = "jobManager.html";
            break
    }
}

/**
 * 隐藏显示加载框
 */
function loading(hide) {
    if (hide) {
        $("#loading").css("display", "none");
    } else {
        $("#loading").css("display", "block");
    }
}